
clear all 

//define folder global here

gl Inputs "$folder\1_Inputs"
gl Intermediate "$folder\2_Intermediate"
gl Outputs "$folder\3_Outputs"

********************************************************************************
********************************************************************************
**# TABLE D.1
import delimited "${Inputs}\Cook_census_matches.csv", clear 

// add pvalues + stars 
foreach i in evicted judgestrin {
	
	gen `i'_zscore = `i'_coeff / `i'_se
	gen `i'_pvalue = 2 * (1-normal(abs(`i'_zscore)))
	
	gen 	`i'_stars = "***" if `i'_pvalue < 0.01
	replace `i'_stars = "**" if `i'_pvalue < 0.05 & mi(`i'_stars)
	replace `i'_stars = "*" if `i'_pvalue < 0.1 & mi(`i'_stars)
	
	gen `i'_coeff2 = string(`i'_coeff, "%10.5fc")
	gen `i'_se2 = string(`i'_se, "%10.5fc")
	
	foreach j in coeff2 se2 {
		replace `i'_`j' = "" if `i'_`j' == "."
		replace `i'_`j' = "0" + `i'_`j' if strpos(`i'_`j', ".") == 1
		replace `i'_`j' = subinstr(`i'_`j', "-.", "-0.", .)
	}
	
	replace `i'_coeff2 = `i'_coeff2 + `i'_stars
	replace `i'_se2 = "(" + `i'_se2 + ")" if !mi(`i'_se2)
}

gen observations2 = "457,000" if observations==457000
assert !mi(observations2)

keep variable *2
rename *2 num*
reshape long num, i(variable) j(label) string

replace label = subinstr(label, "judgestrin", "js", .)
replace label = subinstr(label, "evicted", "e", .)
replace label = subinstr(label, "coeff", "co", .)
replace label = subinstr(label, "observations", "obs", .)

replace variable = subinstr(variable, "pred_", "", .)

levelsof variable, local(vars)
levelsof label, local(labs)

foreach var in `vars' {
	di "`var'"
	local id1 = "`var'"

	foreach lab in `labs' {
		di "`lab'"
		local id2 = "`lab'"
			
			levelsof num if variable =="`var'" & label=="`lab'", clean local(`id1'_`id2')
			di "`id1'_`id2'"
	}
}		


gl x " "	
		
texdoc init "${Outputs}/Supplementary data_Table_D1.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c} \hline \toprule 
tex & \multicolumn{2}{c}{Has PIK} \\
tex	& (1) & (2) \\
tex \cmidrule{2-3}
tex $x Intercept 			& `Constant_e_co' & `Constant_js_co' \\
tex 			 			& `Constant_e_se' & `Constant_js_se' \\
tex $x Evicted 				& `evicted_e_co' & `evicted_js_co' \\
tex 			 			& `evicted_e_se' & `evicted_js_se' \\
tex $x Judge stringency		& `loo_vc_ivsamp100_e_co' & `loo_vc_ivsamp100_js_co' \\ 
tex 						& `loo_vc_ivsamp100_e_se' & `loo_vc_ivsamp100_js_se' \\
tex $x Joint action			& `joint_e_co' & `joint_js_co' \\
tex 			 			& `joint_e_se' & `joint_js_se' \\
tex $x Female (predicted)	& `fem_e_co' & `fem_js_co' \\
tex 			 			& `fem_e_se' & `fem_js_se' \\
tex $x White (predicted)	& `whi_e_co' & `whi_js_co' \\
tex 			 			& `whi_e_se' & `whi_js_se' \\
tex $x Black (predicted)	& `bla_e_co' & `bla_js_co' \\
tex 			 			& `bla_e_se' & `bla_js_se' \\
tex $x Hispanic (predicted)		& `his_e_co' & `his_js_co' \\
tex 			 			& `his_e_se' & `his_js_se' \\
tex $x Tenant without attorney				& `d_prose_e_co' & `d_prose_js_co' \\
tex 						& `d_prose_e_se' & `d_prose_js_se' \\
tex $x Ad damnum (1000s)	& `ad_damnum_e_co' & `ad_damnum_js_co' \\
tex 						& `ad_damnum_e_se' & `ad_damnum_js_se' \\
tex $x Neighborhood poverty rate  & `pct_below_povline_e_co' & `pct_below_povline_js_co' \\
tex 						& `pct_below_povline_e_se' & `pct_below_povline_js_se' \\
tex $x Neighborhood median rent 			& `median_rent_e_co' & `median_rent_js_co' \\
tex 						& `median_rent_e_se' & `median_rent_js_se' \\

tex \midrule
tex $x Number of Observations 	& `Constant_obs' & `Constant_obs' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close




********************************************************************************
********************************************************************************
**# TABLE D.2

import delimited "${Inputs}\Cook_experian_matches.tex", clear
	
	replace v1 = v1 + "," + v2 + "," + v3 if regexm(v1, "observations")
	drop v2 v3 
	rename v1 v 
	drop if _n < 6
	drop if v=="\addlinespace" | v=="\midrule" | v=="\bottomrule" | v=="\end{tabular}"
	
	replace v = subinstr(v, "\sym{", "", .)
	replace v = subinstr(v, "}", "", .)
	replace v = subinstr(v, "\\", "", .)
	split v, parse("&")
	drop v
	replace v1 = trim(v1) 
	replace v2 = trim(v2)
	replace v3 = trim(v3) 
	
	replace v1 = "" if v1=="attorney" | v1=="(1000s)"
	
	gen 	type = "co" 	if !mi(v1)
	replace type = "se" 	if mi(v1)
	replace type = "obs" 	if v1=="Number of observations"
	replace type = "r2" 	if v1=="\(R^{2\)"
	replace type = "mean" 	if v1=="Mean of dep. var."
	drop if type=="r2" | type=="mean"
	drop if mi(v2) & mi(v3)

	replace v1 = "medrent" in 13
	replace v1 = "medrent" in 14
	replace type = "se" in 14
	
	replace v1 = "povrate" in 15 
	replace v1 = "povrate" in 16
	replace type = "se" in 16
	
	rename v1 name 
	replace name = name[_n-1] if mi(name) & type=="se"
	replace name = "x" if type!="co" & type!="se"
	
	reshape long v , i(name type) j(column) string
	
	gen 	nickname = substr(lower(subinstr(name, " ", "", .)), 1, 4)
	replace nickname = subinstr(name, "Filing Year ", "fy", .) if regexm(name, "Year")
	
	replace column = "c" + column
	
	order name nickname type column v
	
isid nickname type column	

levelsof nickname, local(nicks)
levelsof type, local(typs)
levelsof column, local(colus)

foreach nix in `nicks' {
	local id1 = "`nix'"
	
	foreach tyx in `typs' {
		local id2 = "`tyx'"
		
		foreach cox in `colus' {
			local id3 = "`cox'"
			
			levelsof v if nickname=="`nix'" & type=="`tyx'" & column=="`cox'", ///
			clean local(`id1'_`id2'_`id3')
			
			di "`id1'_`id2'_`id3'"
			
		}
	}
}


gl x " "	
	
texdoc init "${Outputs}/Supplementary data_Table_D2.tex", replace force 
tex {\footnotesize \begin{tabular}{l c c} \hline \toprule ///
	& (1) & (2) \\
tex \cmidrule{2-3}

tex $x Evicted 			& `evic_co_c2' & `evic_co_c3' \\
tex 					& `evic_se_c2' & `evic_se_c3' \\

tex $x Judge Stringency	& `judg_co_c2' & `judg_co_c3' \\
tex 					& `judg_se_c2' & `judg_se_c3' \\

tex $x Female (Predicted) 			& `fema_co_c2' & `fema_co_c3' \\
tex 					& `fema_se_c2' & `fema_se_c3' \\

tex $x Joint action		& `join_co_c2' & `join_co_c3' \\
tex 					& `join_se_c2' & `join_se_c3' \\

tex $x Tenant without	& `tena_co_c2' & `tena_co_c3' \\
tex attorney			& `tena_se_c2' & `tena_se_c3' \\

tex $x Ad damnum amount & `adda_co_c2' & `adda_co_c3' \\
tex (1000s)				& `adda_se_c2' & `adda_se_c3' \\

tex $x Neighborhood 	& `povr_co_c2' & `povr_co_c3' \\ 
tex poverty rate 		& `povr_se_c2' & `povr_se_c3' \\

tex $x Neighborhood 	& `medr_co_c2' & `medr_co_c3' \\
tex median rent 		& `medr_se_c2' & `medr_se_c3' \\

tex \midrule
tex $x Number of Observations 	& `x_obs_c2' & `x_obs_c3' \\

tex \bottomrule \\
tex	\end{tabular} }
texdoc close